package com.yuneec.fly.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;

import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.utils.AppManager;
import com.yuneec.fly.utils.inject.ViewUtils;

/**
 * Created by lk.sun on 6/2/2016.
 */
public abstract class BaseActivity extends Activity {
    protected boolean activityRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AppManager.getAppManager().addActivity(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this); // 注入view和事件
    }

    @Override
    protected void onResume() {
        GlobalParams.fragmentManager = getFragmentManager();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        AppManager.getAppManager().finishActivity(this);
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        activityRunning = true;
        if (!EventBusFactory.getUiBus().isRegistered(this)) {
            EventBusFactory.getUiBus().register(this);
        }
        if (!EventBusFactory.getFeedBackBus().isRegistered(this)) {
            EventBusFactory.getFeedBackBus().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        activityRunning = false;
        if (EventBusFactory.getUiBus().isRegistered(this)) {
            EventBusFactory.getUiBus().unregister(this);
        }
        if (EventBusFactory.getFeedBackBus().isRegistered(this)) {
            EventBusFactory.getFeedBackBus().unregister(this);
        }
    }

}
